package ${entity.packageName}.logic.controllers;

import com.gonsin.base.GPageRequest;
import com.gonsin.base.GPageResult;
import com.gonsin.base.ResponseResult;
import com.gonsin.base.exceptions.MyException;
import com.gonsin.venus.base.logic.auth.DeviceApi;
import com.gonsin.venus.base.logic.auth.WebApi;
import com.gonsin.venus.base.logic.keyOnly.KeyOnlyUtils;
import com.gonsin.venus.base.logic.session.SessionKeys;
import ${entity.packageName}.bean.${entity.entityName}Bean;
import ${entity.packageName}.interfaces.I${entity.entityName}Service;
import ${entity.packageName}.po.${entity.entityName}Po;
import ${entity.packageName}.po.${entity.entityName}SearchPo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "${entity.entityLabel}相关接口")
@Slf4j
@RestController
public class ${entity.entityName}Controller {

    @Autowired
    private I${entity.entityName}Service ${entity.entityNameFirstLower}Service;

    @WebApi
    @DeviceApi
    @ApiOperation(value = "${entity.entityLabel} - 查询多个")
    @PostMapping("/restapi/${entity.entityNameFirstLower}s")
    public ResponseResult<List<${entity.entityName}Bean>> search${entity.entityName}s(
            GPageRequest pageRequest,
            @Valid @ApiParam("${entity.entityLabel}搜索对象") ${entity.entityName}SearchPo searchPo) {
        GPageResult<${entity.entityName}Bean> ${entity.entityNameFirstLower}List = ${entity.entityNameFirstLower}Service.search${entity.entityName}(searchPo, pageRequest);
        return ResponseResult.result(${entity.entityNameFirstLower}List.getData());
    }

    @DeviceApi
    @WebApi
    @ApiOperation("${entity.entityLabel} - 主键查询")
    @PostMapping("/restapi/${entity.entityNameFirstLower}s/{${entity.entityNameFirstLower}Key}")
    public ResponseResult<${entity.entityName}Bean> find${entity.entityName}(
            @ApiParam("${entity.entityLabel}的主键") @PathVariable("${entity.entityNameFirstLower}Key") String ${entity.entityNameFirstLower}Key
    ) {
        return ResponseResult.result(${entity.entityNameFirstLower}Service.get${entity.entityName}(${entity.entityNameFirstLower}Key));
    }

    @WebApi
    @DeviceApi
    @ApiOperation("${entity.entityLabel} - 添加")
    @PutMapping("/restapi/${entity.entityNameFirstLower}s")
    public ResponseResult<${entity.entityName}Bean> add${entity.entityName}(
            @RequestBody @ApiParam(value = "${entity.entityLabel}对象") ${entity.entityName}Po ${entity.entityNameFirstLower}Po,
            @ApiIgnore HttpSession session) {
        return ResponseResult.result(${entity.entityNameFirstLower}Service.add${entity.entityName}(${entity.entityNameFirstLower}Po));
    }

    @WebApi
    @DeviceApi
    @ApiOperation("${entity.entityLabel} - 删除")
    @DeleteMapping("/restapi/${entity.entityNameFirstLower}s/{${entity.entityNameFirstLower}Key}")
    public ResponseResult<Void> remove${entity.entityName}(
            @ApiParam("${entity.entityLabel}${entity.entityNameFirstLower}的key") @PathVariable("${entity.entityNameFirstLower}Key") String ${entity.entityNameFirstLower}Key) {
        ${entity.entityNameFirstLower}Service.delete${entity.entityName}(${entity.entityNameFirstLower}Key);
        return ResponseResult.simpleResult();
    }

    @WebApi
    @DeviceApi
    @ApiOperation("${entity.entityLabel} - 批量删除")
    @DeleteMapping("/restapi/${entity.entityNameFirstLower}s")
    public ResponseResult<Void> remove${entity.entityName}(
            @ApiIgnore HttpSession session,
            @ApiParam("${entity.entityLabel}${entity.entityNameFirstLower}的key") @RequestBody ${entity.entityName}SearchPo searchPo) {
        ${entity.entityNameFirstLower}Service.batchDelete${entity.entityName}(searchPo);
        return ResponseResult.simpleResult();
    }

    @WebApi
    @DeviceApi
    @ApiOperation("${entity.entityLabel} - 修改")
    @PutMapping("/restapi/${entity.entityNameFirstLower}s/{${entity.entityNameFirstLower}Key}")
    public ResponseResult<${entity.entityName}Bean> edit${entity.entityName}(
            @ApiParam("${entity.entityLabel}${entity.entityNameFirstLower}的key") @PathVariable("${entity.entityNameFirstLower}Key") String ${entity.entityNameFirstLower}Key,
            @Valid @RequestBody @ApiParam("${entity.entityLabel}对象") ${entity.entityName}Po ${entity.entityNameFirstLower}Po) {
        return ResponseResult.result(${entity.entityNameFirstLower}Service.update${entity.entityName}(${entity.entityNameFirstLower}Key, ${entity.entityNameFirstLower}Po));
    }

}
